//Copyright (c) Microsoft Corporation.  All rights reserved.

using System;
using System.Text;
using System.Collections.Generic;
using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace Eas.WebCrawler.Test.PlugIns {
    /// <summary>
    /// Summary description for CompletedRequestHistoryTest
    /// </summary>
    [TestClass]
    public class CompletedRequestHistoryTest {
        Eas.WebCrawler.PlugIns.Histories.FileBasedRequestHistory _History;
        private static TestContext _Context;
        private static NMock2.Mockery _Mockery;

        public CompletedRequestHistoryTest() {
            //
            // TODO: Add constructor logic here
            //
        }

        #region Additional test attributes
        //
        // You can use the following additional attributes as you write your tests:
        //
        // Use ClassInitialize to run code before running the first test in the class
        // [ClassInitialize()]
        // public static void MyClassInitialize(TestContext testContext) { }
        //
        // Use ClassCleanup to run code after all tests in a class have run
        // [ClassCleanup()]
        // public static void MyClassCleanup() { }
        //
        // Use TestInitialize to run code before running each test 
        [TestInitialize()]
        public void MyTestInitialize() {
            TestDirectoryHelper.Setup();
            _Mockery = new NMock2.Mockery();
            _Context = new TestContext();
            _Context.DelegatedContext =  _Mockery.NewMock<Eas.WebCrawler.Interfaces.WebCrawlerContext>();
            _History = new Eas.WebCrawler.PlugIns.Histories.FileBasedRequestHistory(TestDirectoryHelper.Name(TestDirectory.Source, "Uris.txt"));
            _History.Initialize(_Context);
            _Context._Events.Fire_Starting();
        }

        // Use TestCleanup to run code after each test has run
        [TestCleanup()]
        public void MyTestCleanup() {
            _Context._Events.Fire_Stopped();
            _Mockery.VerifyAllExpectationsHaveBeenMet();
            _History.Dispose();
            _Context.Dispose();
            _Context = null;
            _Mockery.Dispose();
            _Mockery = null;

            TestDirectoryHelper.TearDown();
        }
        //
        #endregion

        [TestMethod]
        /// <summary>Executes the AlwaysEmptyReqeustHistoryTest test.</summary>
        public void AlwaysEmptyReqeustHistoryTest() {
            using (Eas.WebCrawler.PlugIns.Histories.AlwaysEmptyRequestHistory history = new Eas.WebCrawler.PlugIns.Histories.AlwaysEmptyRequestHistory()) {
                history.Initialize(null);
                CheckForEnumeratorEnd(history.GetCompletedRequests().GetEnumerator());
                history.AddCompletedRequest(UriHelper.Google);
                history.AddCompletedRequest(UriHelper.Google);
                history.AddCompletedRequest(UriHelper.Google);
                history.AddCompletedRequest(UriHelper.Yahoo);
                CheckForEnumeratorEnd(history.GetCompletedRequests().GetEnumerator());
            }
        }

        [TestMethod]
        /// <summary>Executes the SimpleFileHistoryTest test.</summary>
        public void SimpleFileHistoryTest() {
            IEnumerator<Uri> uris = CheckFileForBaseItems();
            CheckForEnumeratorEnd(uris);
        }

        [TestMethod]
        /// <summary>Executes the AddToFileHistoryTest test.</summary>
        public void AddToFileHistoryTest() {
            _History.AddCompletedRequest(UriHelper.Google);
            _Context._Events.Fire_Stopped();
            _Context._Events.Fire_Starting();
            IEnumerator<Uri> uris = CheckFileForBaseItems();
            CheckForUri(uris, UriHelper.Google);
            CheckForEnumeratorEnd(uris);
        }


        IEnumerator<Uri> CheckFileForBaseItems() {
            IEnumerator<Uri> uris = _History.GetCompletedRequests().GetEnumerator();
            CheckForUri(uris, UriHelper.Yahoo);
            CheckForUri(uris, UriHelper.Msn);
            CheckForUri(uris, UriHelper.Microsoft);
            return uris;
        }

        void CheckForUri(IEnumerator<Uri> uris, Uri uri) {
            Assert.IsTrue(uris.MoveNext());
            Assert.AreEqual<Uri>(uris.Current, uri);
        }

        void CheckForEnumeratorEnd(IEnumerator<Uri> uris) {
            Assert.IsFalse(uris.MoveNext());
        }
    }
}
